package tcu.iot.gasmanage.mapper;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import tcu.iot.gasmanage.entity.Options;
import tcu.iot.gasmanage.entity.person.Person;
import tcu.iot.gasmanage.entity.person.PersonQuery;
import tcu.iot.gasmanage.mapper.provider.PersonProvider;

import java.util.List;

@Repository
@Mapper
@Component("PersonMapper")
public interface PersonMapper {

    //通过PersonProvider生成的动态sql语句来实现多条件的Person查询
    @SelectProvider(type = PersonProvider.class, method = "SetPersonOrder")
    List<Person> getAllPerson(PersonQuery personQuery);

    //搜索数据库中一共有多少条person数据
    @Select("select count(*) as count from person")
    int count();

    //sql语句，插入数据
    @Insert("INSERT INTO person(id,username,limits,name,management,phone,region,site,time,remark,email,pipeline) VALUES " +
            "(#{id},#{username},#{limits},#{name},#{management},#{phone},#{region},#{site},#{time},#{remark},#{email},#{pipeline})")
    boolean AddPerson(Person person);

    //根据传入的id，删除数据
    @Delete("DELETE from person where id=#{id}")
    boolean DeletePerson(int id);

    //根据传入的修改后的实体类，更新person数据库
    @Update("UPDATE person SET username=#{username},limits=#{limits},name=#{name},management=#{management},phone=#{phone}," +
            "region=#{region},site=#{site},time=#{time},remark=#{remark},email=#{email},pipeline=#{pipeline} WHERE id=#{id}")
    boolean UpdatePerson(Person person);

    //根据生成的sql语句所有perosn数据
    @SelectProvider(type = PersonProvider.class, method = "findPerson")
    @Results({@Result(property = "username", column = "username"), @Result(property = "management", column = "management"),
            @Result(property = "region", column = "region"), @Result(property = "site", column = "site"),
            @Result(property = "pipeline", column = "pipeline")})
    List<Person> SelectPersonByMany(PersonQuery personQuery);

    //根据生成的sql语句判断一共有多少条数据
    @SelectProvider(type = PersonProvider.class, method = "findCountPerson")
    int SelectPersonCount(PersonQuery personQuery);

    /*获取数据库选项信息*/
    @Select("SELECT * from limitsOptions")
    List<Options> FindLimitsOptions();

    @Select("SELECT management FROM equipment WHERE management IS NOT NULL GROUP BY management")
    @Results({@Result(property = "key",column = "management"),@Result(property = "label",column = "management")})
    List<Options> FindManagementOptions();

    @Select("SELECT pipeline FROM equipment WHERE pipeline IS NOT NULL GROUP BY pipeline")
    @Results({@Result(property = "key",column = "pipeline"),@Result(property = "label",column = "pipeline")})
    List<Options> FindPipelineOptions();

    @Select("SELECT region FROM equipment WHERE region IS NOT NULL GROUP BY region")
    @Results({@Result(property = "key",column = "region"),@Result(property = "label",column = "region")})
    List<Options> FindRegionOptions();

    @Select("SELECT site FROM equipment WHERE site IS NOT NULL GROUP BY site")
    @Results({@Result(property = "key",column = "site"),@Result(property = "label",column = "site")})
    List<Options> FindSiteOptions();

    @Select("SELECT principal FROM equipment WHERE principal IS NOT NULL GROUP BY principal")
    @Results({@Result(property = "key",column = "principal"),@Result(property = "label",column = "principal")})
    List<Options> FindPrincipalOptions();
}
